﻿package
{

	import ASF;
	
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;

	import Delay;
	
	import StateGameLoop;
	
	public class ASRock extends FlxSprite
	{
		[Embed(source = 'data/rockhitground.mp3')] private var sndHitGround:Class;
		
		public var _parentState:StateGameLoop;
	
		/*----------------------------------------------*/
		public function ASRock(ParentState:StateGameLoop)
		{
			super(0, 0);
			exists = false;
			visible = false;
			_parentState = ParentState;
		}

		/*----------------------------------------------*/
		public function spawn(X:int = -1, Y:int = -1, break_dir:int = 0):void
		{
			if (break_dir) {
				// 10% chance of not creating a broken rock
				if ((FlxG.random() * 100) < 10) {
					return;
				}
			}
			if (X < 0) {
				x = FlxG.random() * (FlxG.width - width);
				y = -height;
			} else {
				x = X;
				y = Y;
			}
			if (break_dir) {
				velocity.y = Utils.randRange(ASF.MIN_ROCK_BREAK_SPEED, ASF.MAX_ROCK_BREAK_SPEED);
			}
			else {
				velocity.y = Utils.randRange(ASF.MIN_ROCK_DROP_SPEED, ASF.MAX_ROCK_DROP_SPEED);
			}
			
			if (break_dir < 0) {
				velocity.x = Utils.randRange(ASF.MIN_ROCK_BREAK_DRIFT, ASF.MAX_ROCK_BREAK_DRIFT);
			}
			else if (break_dir > 0) {
				velocity.x = -Utils.randRange(ASF.MIN_ROCK_BREAK_DRIFT, ASF.MAX_ROCK_BREAK_DRIFT);
			}
			else {
				// Some random drifting action
				if ( int(FlxG.random() * 5) == 0) {
					var drift_amount:int = Utils.randRange(ASF.MIN_ROCK_DRIFT, ASF.MAX_ROCK_DRIFT);
					
					if (int(FlxG.random() * 2)) {
						velocity.x = drift_amount;
					} else {
						velocity.x = -drift_amount;
					}
				}
			}
						
			exists = true;
			visible = true;

			color = ASF._rock_colors[int(FlxG.random() * ASF._rock_colors.length)];
		}
		
		/*----------------------------------------------*/
		override public function update():void
		{
			if (y > ASF.GROUND_LEVEL) {
				exists = false;
				FlxG.play(sndHitGround, 0.25);
				_parentState.adjustScore(ASF.SCORE_PENALTY_ROCK_SMALL_LANDS);
			}
						
			// Sometimes they trail off the screen due to horizontal velocity -- fuck it, you lucked out
			if (!onScreen()) {
				exists = false;
			}
			super.update();
		}
		
		/*----------------------------------------------*/
		override public function hurt(Damage:Number):void
		{
			exists = false;
			visible = false;
		}
		
		/*----------------------------------------------*/
		public function getScore():int
		{
			return 0;
		}
	}
	
}